import React, { Component } from 'react'
import { Switch, Redirect } from 'react-router'
import { PrivateRoute } from 'components/route'
import { PaddingContainer } from 'components/containers'
import Label from './label'
import Nav from './nav'
import Assign from './assign'
import UserTask from './user'
import AggregateTask from './aggregation'

export default class Org extends Component {
  state = { activeItem: '' }

  componentDidMount = () => {
    const { setParentActiveItem } = this.props
    setParentActiveItem('task')
  }

  setActiveItem = item => this.setState({ activeItem: item })

  render() {
    const { location, match, project } = this.props
    const { activeItem } = this.state
    return (
      <React.Fragment>
        <Nav activeItem={activeItem} project={project} />
        <PaddingContainer>
          <Switch location={location}>
            <Redirect exact path={`${match.url}`} to={`${match.url}/label`} />
            <PrivateRoute
              path={`${match.url}/label`}
              render={props => (
                <Label {...props} setParentActiveItem={this.setActiveItem} project={project} />
              )}
              exact={false}
            />
            <PrivateRoute
              path={`${match.url}/user/:name`}
              render={props => (
                <UserTask {...props} setParentActiveItem={this.setActiveItem} project={project} />
              )}
              exact={false}
            />
            <PrivateRoute
              path={`${match.url}/assign`}
              render={props => (
                <Assign {...props} setParentActiveItem={this.setActiveItem} project={project} />
              )}
              exact={false}
            />
            <PrivateRoute
              path={`${match.url}/aggregation`}
              render={props => (
                <AggregateTask
                  {...props}
                  setParentActiveItem={this.setActiveItem}
                  project={project}
                />
              )}
              exact={false}
            />
            <Redirect from="*" to={`${match.url}`} />
          </Switch>
        </PaddingContainer>
      </React.Fragment>
    )
  }
}
